using System;
using System.Collections.Generic;
using Edge.Common;
using Edge.Common.ComponentModel;
using Edge.Common.ComponentModel.Notification;
using Edge.Common.Services;
using Edge.Common.Utils;
using Edge.DAL.Common;
using Edge.DAL.Common.ResultSet;
using Edge.DAL.Managers;
using Iesi.Collections.Generic;
using NHibernate;
using NHibernate.Expression;
using Shop.Business.Domain.Catalog;
using Shop.Business.Domain.Catalog.Parameter;
using Shop.Business.Domain.Customers;

namespace Shop.DAL.Managers
{
    class ParameterManager : AbstractManager<Parameter, int, Customer>, IParameterManager
    {
       // private ResultSetTransformer<Parameter> parameterTransformer;
        

        public ParameterManager()
        {
            #region Parameter mapper

//            ResultSetMapper<Parameter> pt = new ResultSetMapper<Parameter>();
//            pt.AddMap("Id", "Id");
//            pt.AddMap("Name", "Name");
//            pt.AddMap("Description", "Description");
//            pt.AddMap("IsAtomic", "IsAtomic");
//            pt.AddMap("IsSearch", "IsSearch");
//            pt.AddMap("IsMenu", "IsMenu");
//            parameterTransformer = new ResultSetTransformer<Parameter>(pt);

			#endregion

            
		}

        public ICollection<Parameter> Find(FetchOptions fetchOptions, ParameterFilter filter)
        {
            ICriteria criteria = CreateCriteria(fetchOptions);
            AddRestrictions(criteria, filter);
            return criteria.List<Parameter>();
        }

        public int Count(ParameterFilter filter)
        {
            ICriteria criteria = CreateCriteria();
            AddRestrictions(criteria, filter);
            criteria.SetProjection(Projections.RowCount());
            return (int)criteria.List()[0];
        }

        private void AddRestrictions(ICriteria criteria, ParameterFilter filter)
        {
            if (filter.IsMenu != null)
                criteria.Add(Expression.Eq("IsMenu", filter.IsMenu));
            if (!StringUtils.IsBlankOrNull(filter.GetText()) && !(filter.GetText().Trim().Equals("")))
            {
                criteria.Add(Expression.Disjunction()
                                 .Add(Expression.Like("Name", "%" + filter.GetText().Trim() + "%"))
                                 .Add(Expression.Like("Description", "%" + filter.GetText().Trim() + "%"))
                    );
            }
        }


        public ICollection<Parameter> GetProductParameters(int prodId)
        {
            using (IDalOperation op = CreateLoadOperation())
            {
                IQuery query = Session.GetNamedQuery("GetProductParameters");
                query.SetInt32("productID", prodId);
                ICollection<Parameter> ret = query.List<Parameter>();
                op.Commit();
                foreach (Parameter parameter in ret)
                {
                    parameter.ValuesForProduct = Locator.GetService<IParameterValueManager>().GetProductPatameterValues(prodId, parameter.Id);
                }
                return ret;
            }
        }

        public ICollection<Parameter> GetGroupParameters(int groupId)
        {
            using (IDalOperation op = CreateLoadOperation())
            {
                IQuery query = Session.GetNamedQuery("GetGroupParameters");
                query.SetInt32("groupID", groupId);
                ICollection<Parameter> ret = query.List<Parameter>();
                op.Commit();
                foreach (Parameter parameter in ret)
                {
                    parameter.ValuesForGroup = Locator.GetService<IParameterValueManager>().GetGroupPatameterValues(groupId, parameter.Id);
                }
                return ret;
            }
        }

        public ICollection<Parameter> GetGroupParametersOnly(int groupId)
        {
            using (IDalOperation op = CreateLoadOperation())
            {
                IQuery query = Session.GetNamedQuery("GetGroupParametersOnly");
                query.SetInt32("groupID", groupId);
                ICollection<Parameter> ret = query.List<Parameter>();
                op.Commit();
                foreach (Parameter parameter in ret)
                {
                    parameter.ValuesForGroup = Locator.GetService<IParameterValueManager>().GetGroupPatameterValues(groupId, parameter.Id);
                }
                return ret;
            }
        }

        

        public ICollection<NavigationItem> ConvertToNavigation()
        {
            
            ParameterFilter filter = new ParameterFilter();
            filter.IsMenu = true;
            ICollection<Parameter> filters = Find(FetchOptions.All,filter);
            ICollection<NavigationItem> ret = new List<NavigationItem>();
            foreach (Parameter parameter in filters)
            {
                if (parameter.Values.Count > 0)
                    ret.Add(ConvertToNavigation(parameter));
            }
            return ret;
        }


        public NavigationItem ConvertToNavigation(Parameter parameter)
        {
            NavigationItem main = new NavigationItem(parameter.Name, "par_"+parameter.Id.ToString());
            ICollection<ParameterValue> values = parameter.Values;
            NavigationItem[] child = new NavigationItem[values.Count];
            int i = 0;
            foreach (ParameterValue value in values)
            {
                child[i] = ConvertToNavigation(value);
                i++;
            }
            main.Childrens = child;
            return main;
        }

        public NavigationItem ConvertToNavigation(ParameterValue value)
        {
            NavigationItem ret = new NavigationItem(value.Value, value.Id.ToString());
            return ret;
        }
    }
}